package com.itunion.ddd.common.base.constant;

public enum BaseCode {
    /** 成功状态码 **/
    SUCCESS("00000", "请求成功"),
    /** 参数错误：A0100-A0199 **/
    PARAM_IS_INVALID("A0101","参数无效"),
    PARAM_IS_BLANK("A0102","参数为空"),
    PARAM_TYPE_BIND_ERROR("A0103","参数类型错误"),
    PARAM_NOT_COMPLETE("A0104","参数缺失"),
    PARAM_BLACKID_IS_BLANK("A0105","错误的请求认证信息"),
    PARAM_IS_REPEAT("A0106","参数重复"),

    /**业务验证错误：A0200-A0299**/
    MAX_NUMBER_LIMIT("A0201","数量超过最大限制"),

    /** 接口错误：3001-3999 **/
    FAIL_BLACK("4000", "请求异常！"),
    FAIL("4001", "请求的资源不存在"),
    USER_IS_BLACK("4002", "用户已被加入黑名单！"),
    unknown("5001", "内部服务器错误"),
    NETWORK_IS_ERROR("4003", "当前访问人数过多，请稍后重试！")
    ;
    private String code;
    private String desc;
    BaseCode(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }
    public static BaseCode fromCode(String code) {
        for (BaseCode ec : BaseCode.values()) {
            if (ec.getCode().equals(code)) {
                return ec;
            }
        }
        return unknown;
    }

    public String getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    /**
     * 根据code获取desc
     * @param code
     * @return
     */
    public static String getDescByCode(String code){
        BaseCode[] values= BaseCode.values();
        for (BaseCode value: values) {
            if(value.getCode().equals(code)){
                return value.getDesc();
            }
        }
        return String.valueOf(code);
    }

    /**
     * 根据des获取code
     * @param desc
     * @return
     */
    public static String getCodeByDesc(String desc){
        BaseCode[] values= BaseCode.values();
        for (BaseCode value: values) {
            if (value.getDesc().equals(desc)){
                return value.getCode();
            }
        }
        return String.valueOf(desc);
    }

}
